// node.js, "classic" way:
var MarkdownIt = require('markdown-it'),
  md = new MarkdownIt();
var result = md.render('# markdown-it rulezz!');
console.log(result); // <h1>markdown-it rulezz!</h1>



// node.js, the same, but with sugar:
var md = require('markdown-it')();
var result = md.render('# markdown-it rulezz!');
console.log(result); // <h1>markdown-it rulezz!</h1>
result = md.render('xxx');
console.log(result); // <p>xxx</p>



/** Single line rendering, without paragraph wrap:*/
result = md.renderInline('xxx');
console.log(result); // xxx


/** 所有选项 和 默认配置*/
md = require('markdown-it')({
  html: false, // Enable HTML tags in source
  xhtmlOut: false, // Use '/' to close single tags (<br />).
  breaks: false, // Convert '\n' in paragraphs into <br>

  langPrefix: 'language-',  // CSS language prefix for fenced blocks. Can be useful for external highlighters.
  /*
    fenced blocks 是指 markdown里的这种语法:
    ```
    Sample text here...
    ```
  */

  linkify: false, // Autoconvert URL-like text to links

  // Enable some language-neutral replacement + quotes beautification
  typographer: false,
/*
Enable typographer option to see result.

(c) (C) (r) (R) (tm) (TM) (p) (P) +-
↕转化为
© © ® ® ™ ™ § § ±

test.. test... test..... test?..... test!....
↕转化为
test… test… test… test?.. test!..

!!!!!! ???? ,,  -- ---
↕转化为
!!! ??? , – —

"Smartypants, double quotes" and 'single quotes'
↕转化为
“Smartypants, double quotes” and ‘single quotes’
*/


  // Double + single quotes replacement pairs, when typographer enabled,
  // and smartquotes on. Could be either a String or an Array.
  //
  // For example, you can use '«»„“' for Russian, '„“‚‘' for German,
  // and ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'] for French (including nbsp).
  quotes: '“”‘’',

  // Highlighter function. Should return escaped HTML,
  // or '' if the source string is not changed and should be escaped externally.
  // If result starts with <pre... internal wrapper is skipped. 如果highlight函数的return 以<pre>开头 则内部的代码不会被转译 需要你自己转译 如果你使用 hljs.highlight 则内部会自己进行转译
  highlight: function (/*str, lang*/) { return ''; }
});


/** typographer 和 quotes作用*/
md = require('markdown-it')({
  typographer: true
  , quotes: '«»„“'
});
console.log(md.render(`'a' "b"`)); // <p>„a“ «b»</p>


/** highlight*/
/*
markdown字符串里包含这样的内容才会调用highlight回调
```
Sample text here...
```
*/
md = require('markdown-it')({
  highlight: function (str, lang) {
    console.log('str:', str); // <a>123</a>
    return ''; // 默认值, highlight回调接受到的str会被转译, `<` ->  `&lt;`
  }
});
console.log(md.render(`\`\`\`html\n<a>123</a>\n\`\`\``));
/*
<pre><code class="language-html">&lt;a&gt;123&lt;/a&gt;
</code></pre>
*/

md = require('markdown-it')({
  highlight: function (str, lang) {
    console.log('str:', str); // <a>123</a>
    // return 123; // 报错 TypeError: highlighted.indexOf is not a function
    // return null; // 和 '' 一样, highlight回调接受到的str会被转译
    // return undefined // 同上
    return 'abcdefg' // 如果返回非空字符串 则 highlight回调接受到的str会被替换成这里返回的值
  }
});
console.log(md.render(`\`\`\`html\n<a>123</a>\n\`\`\``));
/*
<pre><code class="language-html">abcdefg</code></pre>
*/


var hljs = require('highlight.js');
md = require('markdown-it')({
  /*highlight: function (str, lang) {
    if (lang && hljs.getLanguage(lang)) {
      try {
        return hljs.highlight(lang, str).value;
      } catch (__) {}
    }

    return ''; // use external default escaping
  }*/
/* ↑这样生成的字符串为(默认code的class为highlight接受到的参数lang与配置项里langPrefix指定的前缀拼接而成):
<pre><code class="language-html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span>&gt;</span>123<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
</code></pre>
*/


  /** 如果想要自定义pre上的class↓*/
  highlight: function (str, lang) {
    console.log('lang:',lang)
    if (lang && hljs.getLanguage(lang)) {
      try {
        return `<pre><code class="hljs ${lang}">${hljs.highlight(lang, str, true).value}</code></pre>`
      } catch (err) {
        console.error(`something wrong with hljs.highlight:`, err);
      }
    }

    return `<pre><code class="hljs">${md.utils.escapeHtml(str)}</code></pre>`;
  }
/* ↑这样生成的字符串为:
<pre class="hljs"><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span>&gt;</span>123<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
</code></pre>
*/
});
console.log(md.render(`\`\`\`html\n<a>123</a>\n\`\`\``));


//TODO https://markdown-it.github.io/markdown-it/
